<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pegawai extends MY_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		if($this->session->userdata('login') === TRUE && in_array(129,$this->access)){
			$log['activity']='VIEW';
			$log['information']='Masuk ke menu : '.$this->uri->uri_string();
			$this->model_log->add($log);
		}else{
			$log['activity']='VIEW';
			$log['information']='Tidak diijinkan : '.$this->uri->uri_string();
			$this->model_log->add($log);
			redirect('sessions/login');
		}
		
		$this->config->set_item("menu_active","pegawai");
		$this->config->set_item("menu_root","pengaturan-awal");
    }
	
	public function index()
	{
		$this->load->model('model_pegawai');
		$data['pegawai']= $this->model_pegawai->get_pegawai();
		
		$params['content'] = $this->load->view('list',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	function topdf($id){
		$mdl = $this->load->model('identitasSekolah/model_identitas_sekolah');
		$data['identitas2']= $mdl->get_identitas_by_id($id);
		 // var_dump($data);
		$this->load->view('cetak_pegawai',$data);
	}
	
	public function form($id=0)
	{
		$this->load->model('model_pegawai');
		$mdl_agama = $this->load->model('agama/model_agama');
		$mdl_suku = $this->load->model('suku/model_suku');
		$mdl_bagian_pegawai = $this->load->model('bagianPegawai/model_bagian_pegawai');
		
		if($id!=0){
			$id = decode($id);
		}		
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			
			$d = $_POST['day'];
			$m = $_POST['month'];
			$y = $_POST['year'];
			$data['tgl_lahir'] = $y."-".$m.'-'.$d;
			
			if(!empty($_FILES) && $_FILES['userfile']['size'] != 0){
				$config['upload_path'] = './temp/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '100';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';

				$this->load->library('upload', $config);

				if ( ! $this->upload->do_upload())
				{
					$this->session->set_flashdata('error',$this->upload->display_errors());
					redirect('pegawai/form/'.encode($id));
				}
				else
				{
					$upload = $this->upload->data();
					$data['foto'] = file_get_contents($upload['full_path']);
					
					if(is_file($upload['full_path']))
						unlink($upload['full_path']);
				}
			}
		
			if($id == 0){	
				$this->model_pegawai->add($data);
				
				$log['activity']='INSERT';
				$log['information']='Insert Pegawai dengan data :'.json_encode($_POST['data']);
			}else{
				$this->model_pegawai->edit($id,$data);			
				$log['activity']='UPDATE';
				$log['information']="UPDATE Pegawai id $id dengan data :".json_encode($_POST['data']);
			}
			
			$this->model_log->add($log);
			redirect('pegawai');
			
			return;
		}
		
		$data['data']= $this->model_pegawai->get_pegawai_by($id);
		$data['agama']= $mdl_agama->get_agama();
		$data['suku']= $mdl_suku->get_suku();
		$data['bagian_pegawai'] = $mdl_bagian_pegawai->get_bagian_pegawai();
		
		$params['content'] = $this->load->view('form',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function delete($id=0){
		
		$id = decode($id);
				
		$this->load->model('model_pegawai');			
		$this->model_pegawai->delete($id);
		
		redirect('pegawai');
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */